package com.szakul.pdf.model;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.vaadin.terminal.StreamResource.StreamSource;

public class PdfImageSource implements StreamSource {

	private ByteArrayOutputStream imagebuffer = null;
	private BufferedImage image;

	public PdfImageSource(BufferedImage image) {
		super();
		this.image = image;
	}

	public InputStream getStream() {

		try {
			/* Write the image to a buffer. */
			imagebuffer = new ByteArrayOutputStream();
			ImageIO.write(image, "png", imagebuffer);

			/* Return a stream from the buffer. */
			return new ByteArrayInputStream(imagebuffer.toByteArray());
		} catch (IOException e) {
			return null;
		}
	}

}
